home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
3_11.lha
/
3_11
/
3_11e.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
1KB
|
54 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ convert an integer into a string of max length len
/ version 2
include <limits.h>
include <swap.h>
include <error.h>
include "3_11e1.c" /* EXPAND defines TWOSCOMPLEMENT */
include "3_11e2.c" /* EXPAND defines BITS() */
include "3_11e3.c" /* EXPAND defines HIBIT() */
oid itoa(char *s, int len, int val)
char *p;
// reverse the sign of negative numbers
if (val < 0)
{
if (--len <= 0)
error("string not long enough");
*s++ = '-';
p = s;
if (TWOSCOMPLEMENT && (val == INT_MIN))
{
if (--len <= 0)
error("string not long enough");
include "3_11e4.c" /* EXPAND12 divides by 10 */
}
else
val = -val;
}
else
p = s;
// store digits in reverse order
do {
if (--len <= 0)
error("string not long enough");
*p++ = val % 10 + '0';
val /= 10;
} while (val != 0);
*p-- = '\0';
// swap the digits around
while (s < p)
swap(*s++, *p--);